<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
slice 1
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

func main() {
	a := [...]int{0, 1, 2, 3}
	x := a[:1]
	y := a[2:]
	x = append(x, y...)
	x = append(x, y...)
	fmt.Println(a, x)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">
<input type="radio" id="choiceD" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">[0 1 2 3] [0 2 3 3 3]</label></li>
<li><label for="choiceB">[0 2 3 3] [0 2 3 3 3]</label></li>
<li><label for="choiceC">[0 1 2 3] [0 2 3 2 3]</label></li>
<li><label for="choiceD">[0 2 3 3] [0 2 3 2 3]</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: [0 2 3 3] [0 2 3 3 3]
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/vDEJUchPeWu">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
Initially, the slice <code class="tmd-code-span">y</code> is <code class="tmd-code-span">[2 3]</code>, and the slice <code class="tmd-code-span">x</code> is <code class="tmd-code-span">[0]</code> with three free element slots (in other words, its capacity is 4),
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
The 1st <code class="tmd-code-span">append</code> call changes the slice <code class="tmd-code-span">x</code> to <code class="tmd-code-span">[0 2 3]</code> with one free element slot. And as the slice <code class="tmd-code-span">x</code> and the array <code class="tmd-code-span">a</code> share elements (before line 10), the array <code class="tmd-code-span">a</code> is changed to <code class="tmd-code-span">[0 2 3 3]</code>. The slice <code class="tmd-code-span">y</code> and the array <code class="tmd-code-span">a</code> also share elements, so the slice <code class="tmd-code-span">y</code> is changed to <code class="tmd-code-span">[3 3]</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
For the slice <code class="tmd-code-span">x</code> has not enough free capacity, the 2nd <code class="tmd-code-span">append</code> call allocates a new backing array for the slice <code class="tmd-code-span">x</code>. So the call doesn't modify the array <code class="tmd-code-span">a</code>. The slice <code class="tmd-code-span">x</code> is changed to <code class="tmd-code-span">[0 2 3 3 3]</code>.
</div>
</li>
</ul>
<p></p>
</div>
<p></p>
</div>
